package com.huixue.cn.ztej.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.huixue.cn.ztej.bean.ConstructionTeam;
import com.huixue.cn.ztej.bean.Results;
import com.huixue.cn.ztej.bean.Section;
import com.huixue.cn.ztej.bean.User;
import com.huixue.cn.ztej.enums.RoleStatus;
import com.huixue.cn.ztej.service.SysConstructionTeamService;
import com.huixue.cn.ztej.service.SysProjectService;
import com.huixue.cn.ztej.utils.SystemContents.API_RETURN_STATUS;
import com.huixue.cn.ztej.utils.annotation.RequestMappingName;
import com.huixue.cn.ztej.utils.framwork.JacksonUtil;
import com.huixue.cn.ztej.utils.framwork.PageParameter;

@Controller
@RequestMapping(value = "/sys/sysProjectMan")
public class ConstructionTeamController extends BaseController {
	
	private static Logger logger = LoggerFactory.getLogger(ConstructionTeamController.class);
			
	@Autowired(required = true)
	SysConstructionTeamService sysConstructionTeamService;
	
	@Autowired(required = true)
	SysProjectService sysProjectService;

	/**
	 * <p>跳转到列表页面</p>
	 */
	@RequestMappingName(value = "跳转到列表页面")
	@RequestMapping(value = "ConstructionInfo/toSearch", method = RequestMethod.GET)
	public String toSearch(@RequestParam(value = "teamCode", defaultValue = "") String teamCode,@RequestParam(value = "page", defaultValue = "1") Integer page,
			@RequestParam(value = "rows", defaultValue = PageParameter.DEFAULT_PAGE_SIZE+"") Integer rows,
			HttpServletRequest request,HttpServletResponse response,Model model) {
		
		Map<String, Object> searchParams = new HashMap<String,Object>();
		searchParams.put("teamCode",teamCode.trim());
		
		User currentUser = (User)request.getSession().getAttribute("loginUser");

		//项目管理
		if(currentUser.getRoleId()!=null && currentUser.getRoleId().equals(RoleStatus.ROLE_8.getRoleId()+"")){
			searchParams.put("proId", currentUser.getProjectID());
		}else{
			Results projectRes =  sysProjectService.findAll(searchParams);
			model.addAttribute("projectRes", projectRes);
		}
		
		String proejctId = request.getParameter("projectId");
		if(proejctId!=null && proejctId.trim().length()>0 && Integer.valueOf(proejctId) >0 ){
			searchParams.put("proId", proejctId);
			model.addAttribute("projectId", proejctId);
		}
		
		searchParams.put("userName", currentUser);
		Results results = sysConstructionTeamService.searchByPage(searchParams,page,rows);
		model.addAttribute("res", results);
		return "/com/ztej/project/sysConsInfo";
	}
	
	
	/**
	 * <p>跳转到添加页面</p>
	 */
	@RequestMappingName(value = "跳转到添加页面")
	@RequestMapping(value = "ConstructionInfo/toAdd", method = RequestMethod.GET)
	public String toAdd(Model model,HttpServletRequest request,HttpServletResponse response) {
		Map<String, Object> searchParams = new HashMap<String,Object>();
		User currentUser = (User)request.getSession().getAttribute("loginUser");

		//项目管理
		if(currentUser.getRoleId()!=null && currentUser.getRoleId().equals(RoleStatus.ROLE_8.getRoleId()+"")){
			searchParams.put("proId", currentUser.getProjectID());
		}
        Results projects = sysProjectService.findAll(searchParams);	
		model.addAttribute("projects", projects);
		return "/com/ztej/project/sysConsInfoAdd";
		
	}
	
	/**
	 * <p>增加项目基本信息</p>
	 */
	@RequestMappingName(value = "增加施工队伍基本信息")
	@RequestMapping(value = "ConstructionInfo/add", method = RequestMethod.POST,consumes="application/json")
	@ResponseBody
	public Results add(@RequestBody ConstructionTeam ct,HttpServletRequest request) {
		
		Results results = null;
		
		results = sysConstructionTeamService.add(ct);
		//默认值
		ct.setSecId(0);
		logger.info(new JacksonUtil().getJson(results));
		
		return results;
	}
	
	/**
	 * <p>跳转到添加页面</p>
	 */
	@RequestMappingName(value = "跳转到更新页面")
	@RequestMapping(value = "ConstructionInfo/toModify/{teamId}", method = RequestMethod.GET)
	public String toModify(@PathVariable("teamId")Integer teamId,Model model,HttpServletRequest request) {
		Results results = sysConstructionTeamService.findBySecId(teamId);
		Map<String, Object> searchParams = new HashMap<String,Object>();
		User currentUser = (User)request.getSession().getAttribute("loginUser");

		//项目管理
		if(currentUser.getRoleId()!=null && currentUser.getRoleId().equals(RoleStatus.ROLE_8.getRoleId()+"")){
			searchParams.put("proId", currentUser.getProjectID());
		}
        Results projects = sysProjectService.findAll(searchParams);
		
		model.addAttribute("projects", projects);
		model.addAttribute("sysConsInfo", results.getData());
		return "/com/ztej/project/sysConsInfoModify";
		
	}
	
	/**
	 * <p>增加项目基本信息</p>
	 */
	@RequestMappingName(value = "修改用户基本信息")
	@RequestMapping(value = "ConstructionInfo/modify", method = RequestMethod.POST)
	@ResponseBody
	public Results modify(@RequestBody ConstructionTeam ct,HttpServletRequest request) {
		
		Results results = null;

		results = sysConstructionTeamService.modify(ct);
		
		logger.info(new JacksonUtil().getJson(results));
		
		return results;
	}
	
	/**
	 * <p>删除工段基本信息</p>
	 * 
	 */
	@RequestMappingName(value = "删除工段基本信息")
	@RequestMapping(value = "ConstructionInfo/delete/{id}", method = RequestMethod.DELETE)
	@ResponseBody
	public Results delete(@PathVariable("id")Integer id,HttpServletRequest request) {
		
		Results results = null;
		try {
			
			results = sysConstructionTeamService.delete(id);
			
		} catch (Exception e) {
			results = new Results(
					API_RETURN_STATUS.SERVER_INTERNAL_ERROR.value(),
					API_RETURN_STATUS.SERVER_INTERNAL_ERROR.desc());
		}
		logger.info(new JacksonUtil().getJson(results));
		return results;
		
	}
	
	
}
